from captcha.fields import CaptchaField
from django import forms

from apps.operation.models import UserProfile
from apps.utils.redis_utitls import getRedisCode



class UpdateMobileForm(forms.Form):
    mobile = forms.CharField(required=True, min_length=11, max_length=11)
    code = forms.CharField(required=True, min_length=4, max_length=4)

    def clean_code(self):
        mobile = self.data.get("mobile")
        code = self.data.get("code")
        email = self.data.get("email")
        redis_code = getRedisCode(email)
        if code != redis_code:
            raise forms.ValidationError("验证码不正确")
        return self.cleaned_data

class ChangePwdForm(forms.Form):
    password1 = forms.CharField(required=True, min_length=5)
    password2 = forms.CharField(required=True, min_length=5)

    def clean(self):
        print(self.data)
        pwd1 = self.data.get("password1")
        pwd2 = self.data.get("password2")

        if pwd1 != pwd2:
            raise forms.ValidationError("密码不一致")
        return self.cleaned_data

class UserInfoForm(forms.ModelForm):
    email=forms.CharField(required=True,max_length=20,min_length=13)
    class Meta:
        model = UserProfile
        fields = ["nick_name", "gender", "birthday", "address"]

class UploadImageForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ["image"]


class RegisterPostForm(forms.Form):
    mobile = forms.CharField(required=True, min_length=11, max_length=11)
    code = forms.CharField(required=True, min_length=4, max_length=4)
    email = forms.CharField(required=True)
    password = forms.CharField(required=True)

    def clean_mobile(self):
        mobile = self.data.get("mobile")
        # 验证手机号码是否已经注册
        users = UserProfile.objects.filter(mobile=mobile)
        if users:
            raise forms.ValidationError("该手机号码已注册")
        return mobile

    def clean_code(self):
        email = self.data.get("email")
        code = self.data.get("code")
        redis_code = getRedisCode(email)
        if code != redis_code:
            raise forms.ValidationError("验证码不正确")

        return code


class RegisterGetForm(forms.Form):
    captcha = CaptchaField()


class DynamicLoginForm(forms.Form):
    mobile = forms.CharField(required=True, min_length=11, max_length=11)
    captcha = CaptchaField()
    email = forms.CharField(required=True)


class LoginForm(forms.Form):
    username = forms.CharField(required=True, min_length=3, max_length=11)
    password = forms.CharField(required=True, max_length=18, min_length=3)


class DynamicLoginPostForm(forms.Form):
    mobile = forms.CharField(required=True, min_length=11, max_length=11)
    code = forms.CharField(required=True, min_length=4, max_length=4)
    email = forms.CharField(required=True)

    def clean_code(self):
        email = self.data.get("email")
        code = self.data.get("code")

        redis_code = getRedisCode(email)
        print(code, redis_code)
        if code != redis_code:
            raise forms.ValidationError("验证码不正确")
        return self.cleaned_data
    # def clean(self):
    #     email=self.cleaned_data["email"]
    #     code=self.cleaned_data["code"]
    #     redis_code=getRedisCode(email)
    #     if code!=redis_code:
    #         raise  forms.ValidationError("验证码不正确")
    #     else:
    #         return self.cleaned_data
